using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
namespace example
{
    class Example
    {
        Microsoft.Win32.OpenFileDialog dialog;
        string filePath;
        public Example()
        {
            dialog = new Microsoft.Win32.OpenFileDialog();
        }
        public string selectCsharpFile()
        {
            dialog.DefaultExt = ".cs";
            dialog.Filter = "C#文件 (*.cs)|*.cs";
            Nullable<bool> result = dialog.ShowDialog();
            if (result == true)
            {
                filePath = dialog.FileName;
                return filePath;
            }
            else
            {
                return "未选择";
            }
        }
        public int[] analyzeFile()
        {
            try
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (!fileInfo.Exists)
                {
                    MessageBox.Show("文件不存在");
                    return null;
                }
                int lineCount = 0;
                int wordCount = 0;
                using (StreamReader reader = new StreamReader(filePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        lineCount++;
                        string[] words = Regex.Split(line, @"\W+");
                        wordCount += words.Length;
                    }
                }
                int[] result = new int[] {lineCount, wordCount };
                return result;
            }
            catch (Exception)
            {
                MessageBox.Show("请先选择文件");
                return null;
            }
        }
        /*
         * （复制文件）删除
         */
        public int[] copCsharpFile()
        {
            try
            {
                if (!File.Exists(filePath))
                {
                    MessageBox.Show("文件不存在");
                    return null;
                }
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
                string destinationFilePath = Path.Combine(Path.GetDirectoryName(filePath), fileNameWithoutExtension + "-copy" + Path.GetExtension(filePath));
                string[] lines = File.ReadAllLines(filePath);
                StringBuilder cleanedText = new StringBuilder();
                foreach (string line in lines)
                {
                    string cleanedLine = Regex.Replace(line, @"\/\/.*", "");
                    if (!string.IsNullOrWhiteSpace(cleanedLine))
                    {
                        cleanedText.AppendLine(cleanedLine);
                    }
                }
                File.WriteAllText(destinationFilePath, cleanedText.ToString());
                string tmp = filePath;
                filePath = destinationFilePath;
                int[] afterResult = analyzeFile();
                filePath = tmp;
                return afterResult;
            }
            catch (Exception)
            {
                MessageBox.Show("请先选择文件");
                return null;
            }
        }
        /*
         * 返回处理后文件的单词列表 
         */
    }
}
